'''
 爬虫处理业务
'''
import requests
from bs4 import BeautifulSoup


class pcScrawler():
    def __init__(self):
        self.headers = {
            'user-agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95'
        }

    # 爬文本
    # def getTargetText(self, url, selector):
    #     a = requests.get(url, headers=self.headers)
    #     a.encoding = 'utf-8'
    #     codes = a.text
    #     bs = BeautifulSoup(codes, 'html.parser')
    #     return [item.text for item in bs.select(selector=selector)]
    def getTargetText(self, url, selector):
        codes = requests.get(url, headers=self.headers).text
        bs = BeautifulSoup(codes, 'html.parser')
        requests.get(url,headers=self.headers).encoding = 'utf-8'
        return [item.text for item in bs.select(selector=selector)]

    # 爬图片
    def getTargetImg(self, imgurl):
        imgc = requests.get(imgurl).content
        with open("2.jpg", 'wb') as f:
            f.write(imgc)


def pa(url1, selector):
    headers = {
        'user-agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTM, like Gecko) Chrome/39.0.2171.95'
    }
    html = requests.get(url1, headers=headers)
    html.encoding = 'utf-8'
    rs = BeautifulSoup(html.text, 'html.parser').select(selector)
    return [item.text for item in rs]


if __name__ == '__main__':
    url = "https://news.cctv.com/2023/04/08/ARTIU5YjRN2zbaaX8y8epdeW230407.shtml"
    s = pcScrawler()
    print(s.getTargetText(url, 'p.span em'))
    imgurl = "https://nimg.ws.126.net/?url=http%3A%2F%2Fdingyue.ws.126.net%2F2023%2F0409%2F49983bb0j00rstoy10048c000m800etg.jpg&thumbnail=660x2147483647&quality=80&type=jpg"
    print(s.getTargetImg(imgurl))
